package com.etooling.pdm.repository;

import com.etooling.framework.repository.BaseRepository;
import com.etooling.pdm.entity.GoodsPropSetting;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface GoodsPropSettingRepository extends BaseRepository<GoodsPropSetting> {


    /**
     * 获取，最大序号，使用产品id
     *
     * @param typeId
     * @return
     */
    @Query(nativeQuery = true, value = "select COALESCE(max(seq), 0) from pdm.t_goods_prop_setting where goods_type_id=?1 and status !='" + GoodsPropSetting.STATUS_DELETE + "' ")
    Integer getMaxSeq(String typeId);


    /**
     * 获取产品属性配置。name，id
     *
     * @param typeId
     * @return
     */
    @Query(nativeQuery = true, value = "select name, id from pdm.t_goods_prop_setting where goods_type_id = ?1 order by seq")
    List<Map<String, Object>> getIdAndName(String typeId);


    /**
     * 统计一个序号的数量，使用产品id和序号
     *
     * @param typeId
     * @param seq
     * @return
     */
    @Query(nativeQuery = true, value = "select count(*) from pdm.t_goods_prop_setting where goods_type_id=?1 and seq=?2 and status !='" + GoodsPropSetting.STATUS_DELETE + "'")
    Integer countSeq(String typeId, Integer seq);


    /**
     * 统计。一个序号的数量，使用id，产品id，序号
     *
     * @param id
     * @param typeId
     * @param seq
     * @return
     */
    @Query(nativeQuery = true, value = "select count(*) from pdm.t_goods_prop_setting where id != ?1 and goods_type_id=?2 and seq=?3 and status !='" + GoodsPropSetting.STATUS_DELETE + "'")
    Integer countSeq(String id, String typeId, Integer seq);


    @Query(nativeQuery = true, value = "select tgps.id, tgps.name, tgpv.code, tgpv.value from pdm.t_goods_prop_setting tgps left join pdm.t_goods_prop_values tgpv on tgps.id = tgpv.setting_id and tgpv.goods_id = ?2 where tgps.goods_type_id = ?1 order by tgps.seq")
    List<Map<String, Object>> getPropList(String typeId, String goodsId);


}
